﻿using System;
using System.IO;
using System.Windows.Forms;
using Bornander.UI;
using GeoFramework;
using LetsDoIt.BO;
using System.Drawing;

namespace LetsDoIt.Controls
{
	public class CaseItem : UserControl, IExtendedListItem
	{
		private Label lblDateTime;
		private Label lblLatCaseItem;
		private Label lblLngCaseItem;
        private PictureBoxEx picCase;

		private CaseInfo caseinfo;
		public CaseInfo Case
		{
			get { return caseinfo; }
			set { caseinfo = value; }
		}

		public CaseItem()
		{
			InitializeComponent();
			SelectedChanged(false);
		}

		public CaseItem(CaseInfo caseinfo)
		{
			this.caseinfo = caseinfo;
			InitializeComponent();

			if (caseinfo.Pictures != null && caseinfo.Pictures.Count > 0)
			{
			    try
			    {
                    using(Bitmap bmp = new Bitmap(Path.Combine(WastesLocalFactory.CasesStorePath(),caseinfo.Pictures[0])))
                    {
                        picCase.Image = bmp;
                    }
			        
			    }
			    catch (Exception ex)
			    {
                    Program.Log.Error(ex.ToString());
			    }
			}
			lblDateTime.Text = caseinfo.CaseDateTime.ToString("yyyy-MM-dd HH:mm:ss");
			lblLatCaseItem.Text = new Latitude(caseinfo.Latitude).ToString();
			lblLngCaseItem.Text = new Longitude(caseinfo.Longitude).ToString();
			SelectedChanged(false);
		}

		#region Implementation of IExtendedListItem

		public void SelectedChanged(bool isSelected)
		{
			if(isSelected)
			{
				this.BackColor = Color.CornflowerBlue;
			}
			else
			{
				this.BackColor = Color.WhiteSmoke;
			}
		}

		public void PositionChanged(int index)
		{
			//throw new System.NotImplementedException();
		}

		#endregion

		private void InitializeComponent()
		{
            this.picCase = new LetsDoIt.Controls.PictureBoxEx();
            this.lblDateTime = new System.Windows.Forms.Label();
            this.lblLatCaseItem = new System.Windows.Forms.Label();
            this.lblLngCaseItem = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // picCase
            // 
            this.picCase.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)));
            this.picCase.BackColor = System.Drawing.Color.Silver;
            this.picCase.Location = new System.Drawing.Point(3, 3);
            this.picCase.Name = "picCase";
            this.picCase.Size = new System.Drawing.Size(122, 122);
            this.picCase.TabIndex = 0;
            // 
            // lblDateTime
            // 
            this.lblDateTime.Font = new System.Drawing.Font("Tahoma", 10F, System.Drawing.FontStyle.Bold);
            this.lblDateTime.Location = new System.Drawing.Point(131, 3);
            this.lblDateTime.Name = "lblDateTime";
            this.lblDateTime.Size = new System.Drawing.Size(300, 37);
            this.lblDateTime.Text = "datetime";
            // 
            // lblLatCaseItem
            // 
            this.lblLatCaseItem.Location = new System.Drawing.Point(131, 40);
            this.lblLatCaseItem.Name = "lblLatCaseItem";
            this.lblLatCaseItem.Size = new System.Drawing.Size(300, 33);
            this.lblLatCaseItem.Text = "lat";
            // 
            // lblLngCaseItem
            // 
            this.lblLngCaseItem.Location = new System.Drawing.Point(131, 73);
            this.lblLngCaseItem.Name = "lblLngCaseItem";
            this.lblLngCaseItem.Size = new System.Drawing.Size(300, 34);
            this.lblLngCaseItem.Text = "lng";
            // 
            // CaseItem
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.BackColor = System.Drawing.Color.WhiteSmoke;
            this.Controls.Add(this.lblLngCaseItem);
            this.Controls.Add(this.lblLatCaseItem);
            this.Controls.Add(this.lblDateTime);
            this.Controls.Add(this.picCase);
            this.Name = "CaseItem";
            this.Size = new System.Drawing.Size(434, 128);
            this.ResumeLayout(false);

		}
		
		public new void Dispose()
		{
			picCase.Dispose();
		}

		int previousClick = 0; // ???? huh? SystemInformation.DoubleClickTime;

		protected override void OnClick(EventArgs e)
		{
			//base.OnClick(e);
			// This is really dumb. We want the event time, not the current
			// time!
			int now = System.Environment.TickCount;
			// A double-click is detected if the the time elapsed
			// since the last click is within DoubleClickTime.
			if (now - previousClick <= SystemInformation.DoubleClickTime)
			{
				// Raise the DoubleClick event.
				if (DoubleClick != null)
					DoubleClick(this, EventArgs.Empty);
				previousClick = 0;
			}
			else
			{
				// Set previousClick to now so that
				// subsequent double-clicks can be detected.
				previousClick = now;
				// Allow the base class to raise the regular Click event.
				base.OnClick(e);
			}
		}

		// Event handling code for the DoubleClick event.

		public new event EventHandler DoubleClick;
		protected new virtual void OnDoubleClick(EventArgs e)
		{
			if (this.DoubleClick != null)
				this.DoubleClick(this, e);
		}

	}
}
